import Vue from 'vue'
import App from './App'
// 引入vuex 状态库
import store from "./store";
// H5引入微信JSSDK
// #ifdef H5
import wx from './static/js/weixin-js-sdk.js'
Vue.prototype.$wx = wx
// #endif
// 带标头的状态栏样式
import cuCustom from './pages/component/cu-custom.vue'
Vue.component('cu-custom', cuCustom)
// 不带标头的状态栏样式
import defaultBar from './pages/component/default-bar.vue'
Vue.component('default-bar', defaultBar)
// 透明状态栏样式
import transCustom from './pages/component/trans-custom.vue'
Vue.component('trans-custom', transCustom)
// 没有更多了~~~
import tuiDivider from './pages/component/divider.vue'
Vue.component('tui-divider', tuiDivider)

Vue.config.productionTip = false

Vue.prototype.$store = store;

// 域名
const baseUrl = 'https://zsapi.miandanshidai.com'
// 定义公共请求
Vue.prototype.$request = (options) => {
	uni.showLoading({
		title: '加载中'
	})
	if (!options.method) {
		options.method = "GET"
	}
	if (!options.header) {
		options.header = {}
	}

	if (!options.data) {
		options.data = {}
	}

	var channel = ''
	// #ifdef APP-PLUS
	channel = 'app'
	// #endif
	// #ifdef MP-WEIXIN
	channel = 'xcx'
	// #endif
	// #ifdef H5
	channel = 'gzh'
	// #endif

	options.data.channel = channel
	// 传入session_key 
	if (options.url != '/login/login' && options.url != '/user/login') {
		options.data.session_key = uni.getStorageSync('auth_token');
	}

	if (!options.success) {
		options.success = function() {}
	}

	if (!options.fail) {
		options.fail = function() {}
	} else {
		var old_success = options.success;
		options.success = function(responce) {
			if (responce.statusCode < 400 && responce.data.status == 1) {
				//在这里保存token
				if (options.url == '/login/login') {
					uni.setStorageSync('auth_token', responce.data.data);
				}
				// 微信授权session_key
				if (options.url == '/user/login') {
					uni.setStorageSync('auth_token', responce.data.data.session_key);
				}
			} else {
				options.fail(responce)
			}
			old_success(responce)
			uni.hideLoading()
		}
		// token失效同一处理
		var old_fail = options.fail
		options.fail = function(responce) {
			if (responce.data.status == -90001) {
				let curUrl = `/${curUrlWithArgs()}`
				curUrl = encodeURIComponent(curUrl)
				uni.redirectTo({
					url: `/pages/login?next=${curUrl}`,
				})
				return
			} else {
				uni.showModal({
					content:responce.data.message,
					showCancel:false
				})
			}
			old_fail(responce)
			setTimeout(function() {
				uni.hideLoading()
			}, 1000);
		}
	}

	uni.request({
		url: baseUrl + options.url,
		data: options.data,
		method:options.method,
		header: options.header,
		success: options.success,
		fail: options.fail,
		complete: options.complete
	});
}


/*获取当前页带参数的url*/
function curUrlWithArgs() {
	var pages = getCurrentPages()
	var currentPage = pages[pages.length - 1]
	var url = currentPage.route
	var options = currentPage.options
	var urlWithArgs = url + '?'

	for (var key in options) {
		var value = options[key]
		urlWithArgs += key + '=' + value + '&'
	}
	urlWithArgs = urlWithArgs.substring(0, urlWithArgs.length - 1)
	return urlWithArgs
}


App.mpType = 'app'
const app = new Vue({
	store,
	...App
})
app.$mount()
